// pages/prefer/user/user.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  
  data: {
    // 学号
    userId:'',
    // 姓名
    username:'',
    // 手机号
    userPhone:'',
    //省
    province_name:"",
    // 市
    city_name:"",
    // 学校
    school_name:"",
    school_id:"",
    //学校弹出层是否展示
    show:false,
    AreaList:{
    },
    student:{}
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this

    wx.getStorage({
      key: "student",
      success (res) {
       
        that.setData({
          student:{
            id:res.data.student_id,
            name:res.data.student_name ,
            phone:res.data.student_phone 
          },
          
        },function(){
          if(that.data.student.id !=null){
            wx.cloud.callFunction({
              name : "gao",
              data:{
                action:"studentById",
                student_id:that.data.student.id
              }
            }).then(res => {
              that.setData({
                userId : res.result.data[0].student_id,
                username:res.result.data[0].student_name,
                userPhone:res.result.data[0].student_phone,
                school_name:res.result.data[0].student_school
              })
            }).catch(err => {
            })
          } 
        })

      }
    })

    
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //学校选择
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },
  
  changeSchool(e){
    console.log( e.detail.values);
    
    // console.log(e.detail.values.length);
    if(e.detail.values[2]!=undefined){
      app.globalData.schoolArr = e.detail.values;
      
    }
    // console.log(this.data.city_name);
  },
  



  // 提交按钮
  submitMessage(){
    
    if(this.data.school_name==""||this.data.userId==""||this.data.username==""||this.data.userPhone==""){
      wx.showToast({
        title: '请将信息填写完整',
        icon:'none',
        duration: 2000
      })
    }else{
      const that = this
      var studnet = {};
      studnet.student_id = that.data.userId;
      studnet.student_name = that.data.username;
      studnet.student_phone = that.data.userPhone;
      studnet.student_school = that.data.school_name;
      // 讲学生基本信息写入缓存
      wx.setStorage({
        key:"student",
        data:studnet
      })
      wx.getStorage({
        key: 'student',
        success (res) {
        }
      })
      wx.cloud.callFunction({
        name : "gao",
        data:{
          action:"editStudent",
          student_id:that.data.userId,
          student_name:that.data.username,
          student_phone:that.data.userPhone,
          student_school:that.data.school_name
        }
      }).then(res => {
        wx.showToast({
          title: '提交成功',
          icon:'none',
          duration: 2000
        })
      }).catch(err => {
      })
    }
  },
  
})